package com.lindroid.androiduilearning.recyclerview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
import android.text.TextUtils;

import java.util.List;

/**
 * @author Lin
 * @date 2018/4/26
 * @function DiffUtil.Callback类
 */

public class DiffCallback extends DiffUtil.Callback {
    private List<String> mOldList, mNewList;

    public DiffCallback(List<String> mOldList, List<String> mNewList) {
        this.mOldList = mOldList;
        this.mNewList = mNewList;
    }

    @Override
    public int getOldListSize() {
        return mOldList.size();
    }

    @Override
    public int getNewListSize() {
        return mNewList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return mNewList.get(newItemPosition).getClass().equals(mOldList.get(oldItemPosition).getClass());
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        String oldStr = mOldList.get(oldItemPosition);
        String newStr = mNewList.get(newItemPosition);
        return TextUtils.equals(oldStr, newStr);
    }

    @Nullable
    @Override
    public Object getChangePayload(int oldItemPosition, int newItemPosition) {
        String oldStr = mOldList.get(oldItemPosition);
        String newStr = mNewList.get(newItemPosition);
        Bundle bundle = new Bundle();
        if (!TextUtils.equals(oldStr, newStr)) {
            bundle.putString("KEY_CONTENT", newStr);
        }

        if (bundle.size() == 0) {
            //没有变化返回空，此时需要完整绑定，Item会出现闪烁的动画
            return null;
        }
        //返回值不为null，此时为局部绑定，Item会刷新局部的数据，不会出现闪烁的动画
        return bundle;
    }



}
